import { Observable } from 'rxjs';
import { environment } from '../../environments/environment';
import { UserModel } from '../pages/system-setting/user/model/user.model';
import { HttpClient } from '@angular/common/http';
import { Injector } from '@angular/core';

export abstract class RyBaseServiceService  {

    base: string = '';
    _http: HttpClient
    protected constructor(private _baseInjector: Injector) {
        this._http = this._baseInjector.get(HttpClient);
    }
    protected setBase(base: string) {
        this.base = base;
    }

    list(queryParams: any): Observable<any> {
        let dateRangeString = '';
        if (queryParams.params){
            if (queryParams.params['beginTime']&& queryParams.params['endTime']){
                dateRangeString = "?params%5BbeginTime%5D="+queryParams.params['beginTime']+"&params%5BendTime%5D="+queryParams.params['endTime'];
            }
        }
        queryParams.params=[];

        const options = {
            params: queryParams
        };
        return this._http.get<any>(environment.baseUrl + this.base + '/list'+dateRangeString, options);
    }

    export(queryParams: any): Observable<any> {
        if (queryParams.params){
            if (queryParams.params['beginTime']&& queryParams.params['endTime']){
                queryParams['params\[beginTime\]']=queryParams.params['beginTime'];
                queryParams['params\[endTime\]']=queryParams.params['endTime'];
            }
        }
        // 创建 FormData 对象
        const formData = new FormData();
        // 将 queryParams 转换为 FormData 格式
        for (const key in queryParams) {
          if (queryParams.hasOwnProperty(key) && key !== 'params') {
            formData.append(key, queryParams[key]);
          }
        }
        return this._http.post<any>(environment.baseUrl + this.base + '/export', formData);
    }

    /**
     * 获取详情
     */
    get(id: string): Observable<any> {
        return this._http.get<any>(environment.baseUrl + this.base + '/'+id, {});
    }

    /**
     * 新增
     */
    add(data: any): Observable<any> {
        return this._http.post<any>(environment.baseUrl + this.base , data);
    }

    /**
     * 编辑
     */
    update(data: any): Observable<any> {
        return this._http.put<UserModel>(environment.baseUrl + this.base , data);
    }

    /**
     * 删除
     */
    delete(id: string): Observable<any> {
        return this._http.delete<UserModel>(environment.baseUrl + this.base +'/'+ id, {});
    }
}
